import 'package:flutter/material.dart';

import 'RoutePath.dart';

/// 自定义底部导航栏
class CustomBottomNavigationBar extends StatefulWidget {
  const CustomBottomNavigationBar({super.key});

  @override
  State<CustomBottomNavigationBar> createState() => _CustomBottomNavigationBarState();
}

class _CustomBottomNavigationBarState extends State<CustomBottomNavigationBar> {
  /// 当前索引
  int _selectedIndex = 0;

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      /// _selectedIndex 索引改变body自动改变
      body: pages[_selectedIndex],
      bottomNavigationBar: Stack(
        clipBehavior: Clip.none,
        children: [
          Container(
            height: 50, // 导航栏高度
            color: Colors.white,
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildBottomNavigationBarItem(Icons.home, 0),
                _buildBottomNavigationBarItem(Icons.add, 1, isSpecial: true),
                _buildBottomNavigationBarItem(Icons.person, 2),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildBottomNavigationBarItem(IconData icon, int index, {bool isSpecial = false}) {
    return GestureDetector(
      onTap: () {
        _onItemTapped(index);
      },
      child: Container(
        height: isSpecial ? 60 : 40,
        width: isSpecial ? 60 : 40,
        decoration: const BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
        ),
        child: Icon(icon, color: _selectedIndex == index ? Colors.amber[800] : Colors.grey),
      ),
    );
  }
}
